/**
 * This file is part of ossecdb-extjs.
 *
 * Copyright (C) 2011 Brendan Johnston
 *
 * Contact: brendan@johnston.net.au
 *
 * Project: http://code.google.com/p/ossecdb-extjs/
 *
 * License: http://www.gnu.org/licenses/gpl-3.0.html
 */

Ext.define('OssecWeb.view.location.Tree', {
	
	extend: 'Ext.tree.Panel',
	
	alias: 'widget.locationtree',
	
	initComponent: function() {

		Ext.apply(this, {
			title: 'Browse',
			store: 'Locations',
			lines: true,
			rootVisible: true,
			listeners: {
				
				checkchange: function (node, checked) {
					
					// Set child nodes checked state.
					node.cascadeBy(function (node) {
						if (node.get('checked') != null) {
							node.set('checked', checked);
						}
					});
					
					// Set parent nodes checked state, where neccessary.
					while (node = node.parentNode) {
						if (node.get('checked') != null) {
							checked = checked && node.findChild('checked', false, true) == null;
							if (checked == node.get('checked')) {
								break;
							} else {
								node.set('checked', checked);
							}
						}
					}
				}
			}
		});

		this.callParent(arguments);
	}
});